package com.example.socket;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.io.IOException;
import java.nio.ByteBuffer;
import java.nio.CharBuffer;
import java.nio.charset.Charset;

public class TestByteBuffer {
    private static final Logger log = LoggerFactory.getLogger(TestByteBuffer.class);
    private static final String XML_HEADER = "<?xml version=\"1.0\"encoding=\"GBK\"?>";
    private static final String XML_CONTENT = "<hnnx_cydbd><trancode>哈哈哈嘻嘻</trancode><bizToken>425CB547-74F4-4912-BEB2-DE3FFBD74805</bizToken><appid>xxxxxxxxxxxxxxxxxx</appid><ruleid>2</ruleid></hnnx_cydbd>";

    public static void main(String[] args) throws IOException, InterruptedException {
//        int read = 0;
//        int total = 0;
        String content = XML_HEADER + XML_CONTENT;
        content = String.format("%08d", content.getBytes(Charset.forName("GBK")).length) + content;
        byte[] bytes = content.getBytes(Charset.forName("GBK"));
        ByteBuffer buffer = ByteBuffer.allocate(bytes.length);
        buffer.put(bytes);

        buffer.flip();
        CharBuffer decode = Charset.forName("GBK").decode(buffer);
        log.info("decode:{}", decode);

    }

}
